package com.orderMgr.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.orderMgr.bean.OfFood;
import com.orderMgr.bean.OfFoodCriteria;
import com.orderMgr.bean.OfRestaurant;
import com.orderMgr.bean.UserOrderFood;
import com.orderMgr.biz.IOrderMgr;
import com.orderMgr.dao.OfFoodDAO;
import com.staticData.StaticObject;

/**
 *@title 标题：
 *@description 描述：
 *@author 作者 ：wuxionghao
 *@createTime 创建时间：2012-1-4下午02:11:32
 *@version 版本：v1.0
 */
public class OrderMgrAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private IOrderMgr orderMgr;

	private OfFoodDAO ofFoodDAO;

	private OfFood food;

	// 取得餐厅信息
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		String result = "";
		List<OfRestaurant> list = StaticObject.getResInfo();
		ServletActionContext.getRequest().setAttribute("resList", list);
		result = "success";
		return result;
	}

	// 取得餐厅食详细信息
	@SuppressWarnings("unchecked")
	public String getRestaurantInfo() {
		Long ofRestaurantId = Long.parseLong(ServletActionContext.getRequest()
				.getParameter("restaurantId").toString());
		if (ofRestaurantId == null) {
			ofRestaurantId = Long.parseLong("1");
		}
		OfFoodCriteria foodCriteria = new OfFoodCriteria();
		foodCriteria.createCriteria().andOfRestaurantIdEqualTo(ofRestaurantId);
		List<OfFood> list = ofFoodDAO.selectByExample(foodCriteria);
		String result = "";
		if (list != null
				|| ServletActionContext.getRequest().getSession().getAttribute(
						"userId") != null) {
			ServletActionContext.getRequest().setAttribute("foods", list);
			result = "success";
		} else {
			result = "failed";
		}
		return result;
	}

	/**显示所有才的信息
	 * @author meishuangyan
	 */
	public String  getAllFoodInfo(){
		String result = "";
		List<List> list = StaticObject.getFoodInfo();
		ServletActionContext.getRequest().setAttribute("foodList", list);
		result = "success";
		
		return result;
	}
	
	
	/**
	 * 显示用户订餐
	 * 
	 * @author meishuangyan
	 */
	@SuppressWarnings("unchecked")
	public String getUserOrderFood() {
		String reslut = "";
		String userName=(String)ServletActionContext.getRequest().getSession().getAttribute("userName");
		if(userName == null || userName.equals("")){
			ServletActionContext.getRequest().setAttribute("msg", "您没有登录。请先登录...");
			reslut = "error";
		}else{
		List<UserOrderFood> list = orderMgr.getUserOrderFood();
		if (list != null) {
			ServletActionContext.getRequest().setAttribute("showOrdered", list);
			reslut = "success";
		} 
		}
		return reslut;
	}
	
	/**
	 * 显示用户当天的点餐
	 */
	public String getTodayUserOrderFood(){
		String result="";
		String userName=(String)ServletActionContext.getRequest().getSession().getAttribute("userName");
		if(userName == null || userName.equals("")){
			ServletActionContext.getRequest().setAttribute("msg", "您没有登录。请先登录...");
			result = "error";
		}else{
			List<UserOrderFood> list= orderMgr.getTodayUserOrderFood();
			if (list != null) {
				ServletActionContext.getRequest().setAttribute("showOrdered", list);
				result = "success";
			} 
		}
		
		return result;
	}

	/**
	 * 用户点餐
	 * 
	 * @return
	 */
	public String userOrder() {
		String result = "";
		String userName = (String) ServletActionContext.getRequest()
				.getSession().getAttribute("userName");
		String userId=ServletActionContext.getRequest().getParameter("ofId");
		if(userName == null){
			ServletActionContext.getRequest().setAttribute("msg", "您没有登录！请先登录...");
			result ="error";
		}else if(userId ==null){
			ServletActionContext.getRequest().setAttribute("msg", "您的操作错误！请您正确的操作...");
			result ="error";
		}else{
			long foodId = Long.parseLong(userId);
			if (orderMgr.userOrderFood(userName, foodId)) {
				result = "success";
			}
		}
		return result;
	}

	/**
	 * 获取今日订餐
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String showTodayOrdered() {
		String result = "";
		List details = orderMgr.showToDayOrdered();
		if (details != null) {
			ServletActionContext.getRequest().setAttribute("showOrdered",
					details);
			List<OfRestaurant> list = StaticObject.getResInfo();
			ServletActionContext.getRequest().setAttribute("resList", list);
			result = "success";
		}
		return result;
	}
	/**
	 * 获取通过用户名称搜索到的点餐
	 * @return
	 */
	public String getOrderByUserName(){
		String result = "";
		String userName = ServletActionContext.getRequest().getParameter("ordername");
		if(userName == null || userName.equals("")){
			ServletActionContext.getRequest().setAttribute("msg", "输入的内容不能为空！请输入... ");
			result = "error";
		}else{
			
			List<UserOrderFood> orderList = orderMgr.getOrderByUserName(userName);
			if(orderList != null && orderList.size()>0){
				ServletActionContext.getRequest().setAttribute("orderList",orderList);
				result = "success";
			}else{
				ServletActionContext.getRequest().setAttribute("msgfaild", "没有你所要查找的数据！");
				result = "faild";
			}
		}
		
		return result;
	}
	/**
	 * 获取通过菜单名称搜索到的点餐
	 * @return
	 */
	public String getOrderByFoodName(){
		String result = "success";
		String foodName = ServletActionContext.getRequest().getParameter("ordername");
		if(foodName == null || foodName.equals("")){
			ServletActionContext.getRequest().setAttribute("msg", "输入的内容不能为空！请输入... ");
			result = "error";
		}else{
			String	ofFoodName= "%"+foodName+"%";
			List<UserOrderFood> orderList = orderMgr.getOrderByFoodName(ofFoodName);
			if(orderList != null && orderList.size()>0){
				ServletActionContext.getRequest().setAttribute("orderList",orderList);
				result = "success";
			}else{
				ServletActionContext.getRequest().setAttribute("msgfaild", "没有你所要查找的数据！");
				result = "faild";
			}
		}
		
		return result ;
	}

	/*
	 * 取得所有的订餐
	 */
	public String showAllOrdered() {
		String reslut = "";
		List details = orderMgr.showAllOrdered();
		if (details != null) {
			ServletActionContext.getRequest().setAttribute("showAllOrdered",
					details);
			List<OfRestaurant> list = StaticObject.getResInfo();
			ServletActionContext.getRequest().setAttribute("resList", list);
			reslut = "success";
		}
		return reslut;
	}

	/**
	 * 删除用户订餐
	 * 
	 * @return
	 */
	public String delOrdered() {
		String result = "";
		String deId=ServletActionContext.getRequest().getParameter("detailId");
		String useId=ServletActionContext.getRequest().getSession().getAttribute("userId").toString();
		
		if(deId ==null ){
			ServletActionContext.getRequest().setAttribute("msg", "您的操作错误！！请重新操作...");
			result ="error";
		}else if(useId ==null){
			ServletActionContext.getRequest().setAttribute("msg", "您没有登录！请您先登录...");
			result ="error";
		} else{
			Long detailId = Long.parseLong(deId);
			Long userId = Long.parseLong(useId);
			if (orderMgr.userDelFood(detailId, userId)) {
				result = "success";
			}
		}
		
		return result;
	}

	/*
	 * 查看所有的菜品
	 */
	public String getAllFood() {
		String result = "success";
		List<OfFood> list = orderMgr.showAllFood();
		if (list != null && list.size() > 0) {
			ServletActionContext.getRequest().setAttribute("FoodList", list);
			result = "success";
		}
		return result;
	}

	/*
	 * 添加菜品
	 */
	public String AddFood() {
		String reslut = "error";
		food = new OfFood();
		String ofFoodName = ServletActionContext.getRequest().getParameter(
				"foodName");
		String ofFoodPic = ServletActionContext.getRequest().getParameter(
				"foodPic");
		String FoodPrice = ServletActionContext.getRequest().getParameter(
				"FoodPrice");
		String RestaurantId = ServletActionContext.getRequest().getParameter(
				"RestaurantId");
		if (ofFoodName == "") {
			ServletActionContext.getRequest().setAttribute("msg",
					"输入的菜单名不能为空！请重新操作！");
			reslut = "error";
		} else if (ofFoodPic == "") {
			ServletActionContext.getRequest().setAttribute("msg",
					"输入的图片名不能为空不能为空！请重新操作！");
			reslut = "error";
		} else if (FoodPrice == "") {
			ServletActionContext.getRequest().setAttribute("msg",
					"输入的菜单价不能为空！请重新操作！");
			reslut = "error";
		} else if (RestaurantId == "") {
			ServletActionContext.getRequest().setAttribute("msg",
					"输入的餐馆ID不能为空！请重新操作！");
			reslut = "error";
		} else {
			food.setOfFoodName(ofFoodName);
			if (ofFoodPic == null && "".equals(ofFoodPic)) {
				food.setOfFoodPic("Food (1).jpg");
			} else {
				food.setOfFoodPic(ofFoodPic);
			}
			int ofFoodPrice = Integer.parseInt(FoodPrice);
			food.setOfFoodPrice(ofFoodPrice);
			long ofRestaurantId = Long.parseLong(RestaurantId);
			food.setOfRestaurantId(ofRestaurantId);
			try {
				boolean r = orderMgr.AddFood(food);
				if (r) {
					reslut = "success";
				}
				reslut = "error";
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return reslut;
	}

	// 删除菜品
	public String deleteFood() {
		String result = "";
		String Id = ServletActionContext.getRequest().getParameter("ofId");
		// 判断id是否为空
		if (Id == null) {
			result = "error";
		} else {
			long ofId = Long.parseLong(Id);
			int reslut = -1;
			reslut = orderMgr.deleteFood(ofId);
			if (reslut > 0) {
				result = "success";
			}
		}
		return result;
	}

	public IOrderMgr getOrderMgr() {
		return orderMgr;
	}

	public void setOrderMgr(IOrderMgr orderMgr) {
		this.orderMgr = orderMgr;
	}

	public OfFoodDAO getOfFoodDAO() {
		return ofFoodDAO;
	}

	public void setOfFoodDAO(OfFoodDAO ofFoodDAO) {
		this.ofFoodDAO = ofFoodDAO;
	}

	public OfFood getFood() {
		return food;
	}

	public void setFood(OfFood food) {
		this.food = food;
	}

}
